package com.kly.springbootinit.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kly.springbootinit.common.BaseResponse;
import com.kly.springbootinit.common.ErrorCode;
import com.kly.springbootinit.common.ResultUtils;
import com.kly.springbootinit.common.UserContext;
import com.kly.springbootinit.exception.ThrowUtils;
import com.kly.springbootinit.model.dto.reservation.ReservationAddRequest;
import com.kly.springbootinit.model.dto.reservation.ReservationQueryRequest;
import com.kly.springbootinit.model.entity.Reservation;
import com.kly.springbootinit.model.entity.User;
import com.kly.springbootinit.model.entity.Vehicle;
import com.kly.springbootinit.model.enums.VehicleStatus;
import com.kly.springbootinit.model.vo.ReservationVO;
import com.kly.springbootinit.model.vo.VehicleVO;
import com.kly.springbootinit.service.ReservationService;
import com.kly.springbootinit.service.UserService;
import com.kly.springbootinit.service.VehicleService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author admin
 * @date 2025/5/23
 */
@RestController
@RequestMapping("/reservations")
public class ReservationController {

    @Resource
    private ReservationService reservationService;

    @Resource
    private UserService userService;

    @Resource
    private VehicleService vehicleService;


    /**
     * 预约
     *
     * @param reservationAddRequest
     * @return
     */
    @PostMapping
    public BaseResponse<ReservationVO> createReservation(@RequestBody ReservationAddRequest reservationAddRequest) {
        ThrowUtils.throwIf(reservationAddRequest == null, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(reservationService.createReservation(reservationAddRequest));
    }


    /**
     * 查询预约记录
     */
    @GetMapping
    public BaseResponse<ReservationVO> getReservation(@RequestParam Long reservationId) {
        ThrowUtils.throwIf(reservationId == null, ErrorCode.PARAMS_ERROR);
        Long userId = UserContext.getUser().getId();
        Reservation reservation = reservationService.lambdaQuery()
                .eq(Reservation::getUserId, userId)
                .eq(Reservation::getId, reservationId)
                .one();
        // 查询用户信息
        ReservationVO reservationVO = BeanUtil.copyProperties(reservation, ReservationVO.class);
        User user = userService.getById(userId);
        reservationVO.setUserVO(userService.getUserVO(user));
        // 查询车辆信息
        Vehicle vehicle = vehicleService.getById(reservationVO.getVehicleId());
        reservationVO.setVehicleVO(BeanUtil.copyProperties(vehicle, VehicleVO.class));
        return ResultUtils.success(reservationVO);
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public BaseResponse<Page<Reservation>> getReservationPage(ReservationQueryRequest reservationQueryRequest) {
        ThrowUtils.throwIf(reservationQueryRequest == null, ErrorCode.PARAMS_ERROR);
        Long id = reservationQueryRequest.getId();
        Long userId = reservationQueryRequest.getUserId();
        Long vehicleId = reservationQueryRequest.getVehicleId();
        Integer status = reservationQueryRequest.getStatus();
        int current = reservationQueryRequest.getCurrent();
        int pageSize = reservationQueryRequest.getPageSize();

        Page<Reservation> page = reservationService.lambdaQuery()
                .eq(id != null, Reservation::getId, id)
                .eq(userId != null, Reservation::getUserId, userId)
                .eq(vehicleId != null, Reservation::getVehicleId, vehicleId)
                .eq(status != null, Reservation::getStatus, status)
                .orderByDesc(Reservation::getCreateTime)
                .page(new Page<>(current, pageSize));


        // 分页查询

        return ResultUtils.success(page);
    }


    /**
     * 取消预约
     */
    @PutMapping("/cancel/{id}")
    @Transactional(rollbackFor = RuntimeException.class)
    public BaseResponse<Boolean> cancelReservation(@PathVariable Long id) {
        ThrowUtils.throwIf(id == null, ErrorCode.PARAMS_ERROR);
        boolean update = reservationService.lambdaUpdate()
                .eq(Reservation::getId, id)
                .set(Reservation::getStatus, 2)
                .update();
        // 修改车辆状态
        // 获取车辆id
        Reservation reservation = reservationService.getById(id);
        boolean update1 = vehicleService.lambdaUpdate()
                .eq(Vehicle::getId, reservation.getVehicleId())
                .eq(Vehicle::getStatus, VehicleStatus.RESERVED.getStatus())
                .set(Vehicle::getStatus, VehicleStatus.RENTED.getStatus())
                .update();

        ThrowUtils.throwIf(!update || !update1, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(true);
    }

}
